﻿using BI.CF.BLL;
using BI.CF.Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BI.CF.Company.Controllers
{
    public class RecordController : Controller
    {
        //
        // GET: /Record/
        /// <summary>
        /// 资产抵押页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            if (Session["Umodel"] == null)
            {
                return Content("<script>location.href='/MyAccount/View_UserLogin'</script>");
            }
            else
            {
                return View();
            }
        }
        static string urlPath = string.Empty;
        public RecordController()
        {
            var applicationPath = VirtualPathUtility.ToAbsolute("~") == "/" ? "" : VirtualPathUtility.ToAbsolute("~");
            urlPath = applicationPath + "/Upload";


        }
        /// <summary>
        /// 上传方法
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="lastModifiedDate"></param>
        /// <param name="size"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public ActionResult UpLoadProcess(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file)
        {
            string filePathName = string.Empty;

            string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "Upload");
            if (Request.Files.Count == 0)
            {
                return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "保存失败" }, id = "id" });
            }
            string ex = Path.GetExtension(file.FileName);
            filePathName = Guid.NewGuid().ToString("N") + ex;
            if (!System.IO.Directory.Exists(localPath))
            {
                System.IO.Directory.CreateDirectory(localPath);
            }
            file.SaveAs(Path.Combine(localPath, filePathName));

            var url = "";
            url = urlPath + "/" + filePathName;
            Record_BLL bll = new Record_BLL();
            RecordModel m = new RecordModel();
            m.LoanMoney = decimal.Parse(Request["Monwy"].ToString());
            m.LoanPurpose = Request["LoanPurpose"].ToString();
            m.LoanRaise = int.Parse(Request["LoanRaise"].ToString());
            m.Annualrate = float.Parse(Request["Annualrate"].ToString());
            m.LoanTerm = int.Parse(Request["LoanTerm"].ToString());
            m.RepaymentModeId = Request["RepaymentModeId"].ToString();
            m.Title = Request["Title"].ToString();
            m.LoanExplain = Request["LoanExplain"].ToString();
            m.UserrId = int.Parse(Request["UserID"].ToString());
            m.LaunchLoanTime = DateTime.Now;

            m.EndLoanTime = m.LaunchLoanTime.AddDays(m.LoanRaise);
            m.RepaymentDay = DateTime.Now;
            m.PayInterest = Convert.ToDecimal(m.Annualrate / 100 * float.Parse(m.LoanMoney.ToString()) / 12 * m.LoanTerm);
            m.RepaySumMoney = m.LoanMoney + m.PayInterest;
            m.ExamineState = "0";
            m.AlreadyRaiseLoan = 0;
            m.TotalTransactions = 0;
            m.LoanType = Request["LoanType"].ToString();
            m.AssetsCertification = url;
            m.Status = false;
            if (bll.Memloand_LeiADD(m) != true)
            {
                return Content("<script>alert('失败')</script>");

            }
            else
            {
                return Json(new
                {
                    jsonrpc = "2.0",
                    id = id,
                    filePath = urlPath + "/" + filePathName
                });
            }

        }
    }
}
